package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;


/**
 * @BelongsProject: lease
 * @BelongsPackage: com.atguigu.lease.web.admin.custom.converter
 * @Author: jiangyi
 * @CreateTime: 2025-08-16  11:42
 * @Description: TODO
 * @Version: 1.0
 */
@Component // 需要添加到容器中（需要实现WebMvcConfigurer）
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    // 此时是单独针对 Strin2ItemType 的转化器
    @Override
    public ItemType convert(String code) {
        // ItemType.values() 获取所有枚举项  code = "1" itemType.getCode() = 1
        for (ItemType itemType : ItemType.values()) {
            if (itemType.getCode().equals(Integer.valueOf( code))) {
                return itemType;
            }
        }

        throw new RuntimeException("请求参数错误");
    }
}
